<script setup lang="ts">
import ConsultItem from './ConsultItem.vue'
import type { ConsultType } from '@/enums'
import { defineProps, ref } from 'vue'
import type { ConsultOrderListParams, ConsultOrderItem } from '@/types/consult'
import { getConsultOrderList } from '@/services/consult'

const props = defineProps<{
  type: ConsultType
}>()


const params = ref<ConsultOrderListParams>({
  type: props.type,
  current: 1,
  pageSize: 5
})
const loading = ref(false) //加载状态
const finished = ref(false) //是否加载完毕
const list = ref<ConsultOrderItem>([]) //问诊记录列表

const onLoad = async () => {
  const res = await getConsultOrderList(params.value)
  list.value.push(...res.data.rows) //累加

  // van-list组件在下拉加载时没有数据时继续请求，否则展示加载完成
  if (params.value.current < res.data.pageTotal) {
    params.value.current++
  } else {
    finished.value = true //加载完毕
  }
  loading.value = false //加载状态
}

// 删除订单
const onDelete = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
  if (!list.value.length) onLoad()
}
</script>

<template>
  <div class="consult-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <ConsultItem v-for="item in list" :key="item.id" @on-delete="onDelete" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
